#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
ll fac[N], ifac[N];
ll fastpow(ll b, ll p, ll mod) {
  ll res = 1;
  while (p) {
    if (p & 1) res = res * b % mod;
    b = b * b % mod;
    p >>= 1;
  }
  return res;
}
void init(int p) {
  fac[0] = 1;
  rep(i, 1, p - 1) fac[i] = fac[i - 1] * i % p;
  ifac[p - 1] = fastpow(fac[p - 1], p - 2, p);
  per(i, p - 2, 0) ifac[i] = ifac[i + 1] * (i + 1) % p;
}
ll cmb(ll n, ll m, ll p) {
  if (m > n) return 0;
  return fac[n] * ifac[m] % p * ifac[n - m] % p;
}
ll lucas(int n, int m, int p) {
  if (m > n) return 0;
  if (n == m) return 1;
  return lucas(n / p, m / p, p) * cmb(n % p, m % p, p) % p;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, m, p;
    cin >> n >> m >> p;
    init(p);
    cout << lucas(n + m, n, p) << endl;
  }
  return 0;
}